package com.ttc.ssm.service.home.impl;

import com.ttc.ssm.common.ManagePage;
import com.ttc.ssm.common.ResumePage;
import com.ttc.ssm.common.SearchResumePage;
import com.ttc.ssm.controller.common;
import com.ttc.ssm.mapper.admin.*;
import com.ttc.ssm.mapper.admin.CompetitionExpMapper;
import com.ttc.ssm.mapper.admin.EducationMapper;
import com.ttc.ssm.mapper.admin.SkillMapper;
import com.ttc.ssm.mapper.admin.WorkExpMapper;
import com.ttc.ssm.mapper.home.*;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.home.HomeResumeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 58296 on 2016/8/9.
 */
public class HomeResumeServiceImpl implements HomeResumeService{

    @Autowired
    HomeResumeMapper homeResumeMapper;

    @Autowired
    WorkExpMapper workExpMapper;

    @Autowired
    EducationMapper educationMapper;

    @Autowired
    CompetitionExpMapper competitionExpMapper;

    @Autowired
    SkillMapper skillMapper;

    @Autowired
    DegreeMapper degreeMapper;

    @Autowired
    SalaryTypeMapper salaryTypeMapper;

    @Autowired
    IndustryMapper industryMapper;

    @Autowired
    JobPropertyMapper jobPropertyMapper;

    @Autowired
    JobTagMapper jobTagMapper;


    @Autowired
    HomeDeliveryMapper homeDeliveryMapper;

    @Autowired
    HomeInviteMapper homeInviteMapper;

    @Autowired
    HomeDeliveryActivityMapper homeDeliveryActivityMapper;

    @Autowired
    HomeInviteActivityMapper homeInviteActivityMapper;

    @Override
    public int insertResumeService(Resume resume) throws Exception {
        Date date = new Date();
        resume.setDate_added(date);
        resume.setDate_modified(date);
        resume.setSerial_num(common.getSerialNum());
        homeResumeMapper.insertResume(resume);
        return resume.getResume_id();
    }

    @Override
    public int totalResumeRows(ResumePage page) throws Exception {
        return homeResumeMapper.totalResumeRows(page);
    }

    @Override
    public List<Resume> selectResumeForPage(ResumePage page) throws Exception {
        return homeResumeMapper.selectResumeForPage(page);
    }

    @Override
    public Resume selectResumeById(Integer resume_id) throws Exception {
        return homeResumeMapper.selectResumeById(resume_id);
    }

    @Override
    public List<WorkExp> selectWorkByResumeId(int resumeId) throws Exception {
        return workExpMapper.selectWorkByResumeId(resumeId);
    }

    @Override
    public List<Education> selectEduByResumeId(int resumeId) throws Exception {
        return educationMapper.selectEduByResumeId(resumeId);
    }

    @Override
    public List<CompetitionExp> selectComByResumeId(int resumeId) throws Exception {
        return competitionExpMapper.selectComByResumeId(resumeId);
    }

    @Override
    public List<Skill> selectSkillByResumeId(int resumeId) throws Exception {
        return skillMapper.selectSkillByResumeId(resumeId);
    }

    @Override
    public List<Degree> getDegree() throws Exception {
        return degreeMapper.getDegree();
    }

    @Override
    public List<SalaryType> getSalaryType() throws Exception {
        return salaryTypeMapper.getSalaryType();
    }

    @Override
    public List<Industry> getIndustry() throws Exception {
        return industryMapper.getIndustry();
    }

    @Override
    public List<JobProperty> getJobProperty() throws Exception {
        return jobPropertyMapper.getJobProperty();
    }

    @Override
    public List<JobTag> getJobTab(int company_id) throws Exception {
        return jobTagMapper.getJobTag(company_id);
    }

    @Override
    public int countRowsForSearchResume(SearchResumePage searchResumePage) throws Exception {
        return homeResumeMapper.countRowsForSearchResume(searchResumePage);
    }

    @Override
    public List<Resume> selectResume(SearchResumePage searchResumePage) throws Exception {
        return homeResumeMapper.selectResume(searchResumePage);
    }


    @Override
    public int selectCustomerIdByResumeId(int resume_id) throws Exception {
        return homeResumeMapper.selectCustomerIdByResumeId(resume_id);
    }

    @Override
    public List<Resume> selectResumeManageByJobId(ManagePage managePage) throws Exception {
        return homeResumeMapper.selectResumeManageByJobId(managePage);
    }

    @Override
    public int countResumeManageRows(ManagePage managePage) throws Exception {
        return homeResumeMapper.countResumeManageRows(managePage);
    }

    @Override
    public List<Resume> selectResumeManageByJobId2(ManagePage managePage) throws Exception {
        return homeResumeMapper.selectResumeManageByJobId2(managePage);
    }

    @Override
    public int countResumeManageRows2(ManagePage managePage) throws Exception {
        return homeResumeMapper.countResumeManageRows2(managePage);
    }

    @Override
    public void updateDeliveryStatus(int delivery_id, int status) throws Exception {
        Map map = new HashMap();
        map.put("delivery_id", delivery_id);
        map.put("status",status);
        homeDeliveryMapper.updateStatus(map);

        Delivery delivery = homeDeliveryMapper.selectDeliveryById(delivery_id);
        DeliveryActivity deliveryActivity = new DeliveryActivity();
        deliveryActivity.setDelivery_id(delivery.getDelivery_id());
        deliveryActivity.setCustomer_id(delivery.getCustomer_id());
        deliveryActivity.setCompany_id(delivery.getCompany_id());
        deliveryActivity.setStatus(status);
        deliveryActivity.setType(0);
        deliveryActivity.setIs_read(0);
        deliveryActivity.setDate_added(new Date());
        homeDeliveryActivityMapper.insertActivity(deliveryActivity);
    }

    @Override
    public void insertInvite(Invite invite) throws Exception {
        invite.setDate_added(new Date());
        invite.setStatus(0);
        homeInviteMapper.insertInvite(invite);

        InviteActivity inviteActivity = new InviteActivity();
        inviteActivity.setInvite_id(invite.getInvite_id());
        inviteActivity.setCustomer_id(invite.getCustomer_id());
        inviteActivity.setCompany_id(invite.getCompany_id());
        inviteActivity.setStatus(0);
        inviteActivity.setType(0);
        inviteActivity.setIs_read(0);
        inviteActivity.setDate_added(new Date());
        homeInviteActivityMapper.insertActivity(inviteActivity);
    }

    @Override
    public int checkIsSend(int resume_id, int job_id) throws Exception {
        Map map = new HashMap();
        map.put("resume_id", resume_id);
        map.put("job_id", job_id);
        return homeInviteMapper.checkIsSend(map);
    }

}
